package com.aifeng.hassan_food.mine

import android.os.Bundle
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.base.oneClick
import com.aifeng.common_ui.helper.ActivityStack
import com.aifeng.common_ui.helper.clearShared
import com.aifeng.common_ui.helper.getString
import com.aifeng.common_ui.helper.isLogin
import com.aifeng.hassan_food.R
import com.aifeng.hassan_food.WebActivity
import com.aifeng.utils.DialogHelper.showHintDialog
import com.aifeng.utils.GlideCacheUtil
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.activity_setting.*
import org.jetbrains.anko.startActivity

class SettingActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)
        initTitle(title = "设置")
    }

    override fun onStart() {
        super.onStart()
        setting_real.setRightString(if (getString("realName").isNotEmpty()) "已认证" else "去认证")
        setting_cache.setRightString(GlideCacheUtil.getInstance().getAllCacheSize(baseContext))
    }

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        setting_info.oneClick { startActivity<InfoActivity>() }
        setting_real.oneClick {
            if (getString("realName").isEmpty()) {
                startActivity<RealActivity>()
            }
        }
        setting_login.oneClick { startActivity<SettingLoginActivity>() }
        setting_phone.oneClick { startActivity<SettingPhoneActivity>() }
        setting_pay.oneClick {
            if (getString("realName").isEmpty()) {
                showHintDialog(
                    title = "实名认证",
                    hint = "您还未进行实名认证，是否现在去认证？",
                    sure = "去认证"
                ) {
                    startActivity<RealActivity>()
                }
            } else {
                startActivity<SettingPayActivity>()
            }
        }
        setting_about.oneClick { startActivity<AboutActivity>() }
        setting_deal.oneClick { startActivity<WebActivity>("title" to "用户协议") }
        setting_secret.oneClick { startActivity<WebActivity>("title" to "隐私政策") }
        setting_cache.oneClick {
            showHintDialog(
                title = "清空缓存",
                hint = "确定要清空缓存吗？"
            ) {
                GlideCacheUtil.getInstance().clearImageAllCache(baseContext)
                setting_cache.setRightString("0B")
            }
        }
        bt_quit.oneClick {
            showHintDialog(
                title = "退出登录",
                hint = "确定要退出当前账户？"
            ) {
                clearShared(
                    "userId",
                    "nickName",
                    "shopName",
                    "headImg",
                    "shopId",
                    "balance",
                    "openId",
                    "realName",
                    "bankCard",
                    "bindId",
                    "bindImg",
                    "bindName",
                    "createData"
                )
                isLogin = false

                LiveEventBus.get("loginStatus").post("quit")
                LiveEventBus.get("mainLogin").post("quit")
                ActivityStack.screenManager.popActivities(this::class.java)
            }
        }
    }

}
